import { LOGIN_STATUS, UID, PLATFORM } from "../../config/cache";
import Cache from "../../utils/cache";
import { USER_INFO } from "../../config/cache";

const state = {
  token: Cache.get(LOGIN_STATUS) || "",
  backgroundColor: "#fff",
  userInfo: Cache.get(USER_INFO) ? JSON.parse(Cache.get(USER_INFO)) : null,
  uid: Cache.get(UID) || null,
  homeActive: false,
  chatUrl: Cache.get("chatUrl") || "",
  systemPlatform: Cache.get(PLATFORM) ? Cache.get(PLATFORM) : "",
  productType: Cache.get("productType"),
};
const mutations = {
  LOGIN(state, opt) {
    state.token = opt.token;
    state.userInfo = opt.userInfo;
    Cache.set(USER_INFO, opt.userInfo);
    Cache.set(LOGIN_STATUS, opt.token);
  },
  SETUID(state, val) {
    state.uid = val;
    Cache.set(UID, val);
  },
  UPDATE_LOGIN(state, token) {
    state.token = token;
  },
  UPDATE_USERINFO(state, userInfo) {
    state.userInfo = userInfo;
    Cache.set(USER_INFO, userInfo);
  },
  LOGOUT(state) {
    state.token = undefined;
    state.uid = undefined;
    Cache.clear(LOGIN_STATUS);
    Cache.clear(UID);
    Cache.clear(USER_INFO);
  },
  BACKGROUND_COLOR(state, color) {
    state.color = color;
    document.body.style.backgroundColor = color;
  },
  OPEN_HOME(state) {
    state.homeActive = true;
  },
  CLOSE_HOME(state) {
    state.homeActive = false;
  },
  SET_CHATURL(state, chatUrl) {
    state.chatUrl = chatUrl;
  },

  SYSTEM_PLATFORM(state, systemPlatform) {
    state.systemPlatform = systemPlatform;
    Cache.set(PLATFORM, systemPlatform);
  },
};

const actions = {};

export default {
  state,
  mutations,
  actions,
};
